<?php

namespace SLN0002\Controller;

use Home\Common\FIdConst;
use Home\Controller\PSIBaseController;
use Home\Service\UserService;
use SLN0002\Service\AccQueryService;

/**
 * 账簿查询Controller
 *
 * @author PSI
 * @copyright 2015 - present
 * @license GPL v3
 */
class AccQueryController extends PSIBaseController
{
  /**
   * 总分类账查询 - 主页面
   * 
   * 模板页面：web\Application\SLN0002\View\AccQuery\accGlIndex.html
   */
  public function accGlIndex()
  {
    $us = new UserService();

    if ($us->hasPermission(FIdConst::GL_ACC_QUERY_GL)) {
      $this->initVar();

      $this->assign("title", "总分类账查询");

      $this->display();
    } else {
      $this->gotoLoginPage("/SLN0002/AccQuery/accGlIndex");
    }
  }

  /**
   * 明细分类账查询 - 主页面
   * 
   * 模板页面：web\Application\SLN0002\View\AccQuery\accDetailIndex.html
   */
  public function accDetailIndex()
  {
    $us = new UserService();

    if ($us->hasPermission(FIdConst::GL_ACC_QUERY_DETAIL)) {
      $this->initVar();

      $this->assign("title", "明细分类账查询");

      // 按钮权限：保存列视图布局
      $this->assign("pSaveLayout", $us->hasPermission(FIdConst::GL_ACC_QUERY_DETAIL_SAVE_LAYOUT) ? 1 : 0);


      $this->display();
    } else {
      $this->gotoLoginPage("/SLN0002/AccQuery/accDetailIndex");
    }
  }

  /**
   * 查询明细账簿列表
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\AccQuery\SelectAccForm.js
   */
  public function queryAccList()
  {
    if (IS_POST) {
      $us = new UserService();
      if (!$us->hasPermission(FIdConst::GL_KEEP_DETAIL)) {
        die("没有权限");
      }

      $params = [
        "companyId" => I("post.companyId"),
        "subjectCode" => I("post.subjectCode"),
      ];

      $service = new AccQueryService();
      $this->ajaxReturn($service->queryAccList($params));
    }
  }

  /**
   * 返回所有的公司列表
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\AccQuery\DetailMainForm.js
   *     web\Public\Scripts\PSI\SLN0002\AccQuery\SelectAccForm.js
   */
  public function companyList()
  {
    if (IS_POST) {
      $us = new UserService();

      $hasPermission = $us->hasPermission(FIdConst::GL_ACC_QUERY_DETAIL);
      if (!$hasPermission) {
        die("没有权限");
      }

      $service = new AccQueryService();
      $this->ajaxReturn($service->companyList(FIdConst::GL_ACC_QUERY_DETAIL));
    }
  }

  /**
   * 返回所有的公司列表 - 总分类账查询用
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\AccQuery\GlMainForm.js
   */
  public function companyListForGL()
  {
    if (IS_POST) {
      $us = new UserService();

      $hasPermission = $us->hasPermission(FIdConst::GL_ACC_QUERY_GL);
      if (!$hasPermission) {
        die("没有权限");
      }

      $service = new AccQueryService();
      $this->ajaxReturn($service->companyList(FIdConst::GL_ACC_QUERY_GL));
    }
  }

  /**
   * 查询一级子账簿
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\AccQuery\SelectAccForm.js
   */
  public function querySubAcc01()
  {
    if (IS_POST) {
      $us = new UserService();

      $hasPermission = $us->hasPermission(FIdConst::GL_ACC_QUERY_DETAIL);
      if (!$hasPermission) {
        die("没有权限");
      }

      $params = [
        "companyId" => I("post.companyId"),
        "accNumber" => I("post.accNumber"),
        "year" => I("post.year"),
        "month" => I("post.month"),
        "start" => I("post.start"),
        "limit" => I("post.limit"),
      ];

      $service = new AccQueryService();
      $this->ajaxReturn($service->querySubAcc01($params));
    }
  }

  /**
   * 查询二级子账簿
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\AccQuery\SelectAccForm.js
   */
  public function querySubAcc02()
  {
    if (IS_POST) {
      $us = new UserService();

      $hasPermission = $us->hasPermission(FIdConst::GL_ACC_QUERY_DETAIL);
      if (!$hasPermission) {
        die("没有权限");
      }

      $params = [
        "companyId" => I("post.companyId"),
        "accNumber" => I("post.accNumber"),
        "year" => I("post.year"),
        "month" => I("post.month"),
        "subAcc01" => I("post.subAcc01"),
        "start" => I("post.start"),
        "limit" => I("post.limit"),
      ];

      $service = new AccQueryService();
      $this->ajaxReturn($service->querySubAcc02($params));
    }
  }

  /**
   * 查询三级子账簿
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\AccQuery\SelectAccForm.js
   */
  public function querySubAcc03()
  {
    if (IS_POST) {
      $us = new UserService();

      $hasPermission = $us->hasPermission(FIdConst::GL_ACC_QUERY_DETAIL);
      if (!$hasPermission) {
        die("没有权限");
      }

      $params = [
        "companyId" => I("post.companyId"),
        "accNumber" => I("post.accNumber"),
        "year" => I("post.year"),
        "month" => I("post.month"),
        "subAcc02" => I("post.subAcc02"),
        "start" => I("post.start"),
        "limit" => I("post.limit"),
      ];

      $service = new AccQueryService();
      $this->ajaxReturn($service->querySubAcc03($params));
    }
  }

  /**
   * 查询明细账的账样列
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\AccQuery\DetailMainForm.js
   */
  public function queryFmtColsForDetailAcc()
  {
    if (IS_POST) {
      $us = new UserService();

      $hasPermission = $us->hasPermission(FIdConst::GL_ACC_QUERY_DETAIL);
      if (!$hasPermission) {
        die("没有权限");
      }

      $params = [
        "companyId" => I("post.companyId"),
        "accNumber" => I("post.accNumber"),
        "subAccLevel" => I("post.subAccLevel"),
      ];

      $service = new AccQueryService();
      $this->ajaxReturn($service->queryFmtColsForDetailAcc($params));
    }
  }

  /**
   * 查询明细账数据
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\AccQuery\DetailMainForm.js
   */
  public function queryAccDetailData()
  {
    if (IS_POST) {
      $us = new UserService();

      $hasPermission = $us->hasPermission(FIdConst::GL_ACC_QUERY_DETAIL);
      if (!$hasPermission) {
        die("没有权限");
      }

      $params = [
        "companyId" => I("post.companyId"),
        "accNumber" => I("post.accNumber"),
        "year" => I("post.year"),
        "month" => I("post.month"),
        "subAccLevel" => I("post.subAccLevel"),
        "subAcc01" => I("post.subAcc01"),
        "subAcc02" => I("post.subAcc02"),
        "subAcc03" => I("post.subAcc03"),
        "start" => I("post.start"),
        "limit" => I("post.limit"),
      ];

      $service = new AccQueryService();
      $this->ajaxReturn($service->queryAccDetailData($params));
    }
  }

  /**
   * 保存账样列视图
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\AccQuery\DetailMainForm.js
   */
  public function saveViewLayout()
  {
    if (IS_POST) {
      $us = new UserService();

      if (!$us->hasPermission(FIdConst::GL_ACC_QUERY_DETAIL_SAVE_LAYOUT)) {
        die("没有权限");
      }

      $params = [
        "companyId" => I("post.companyId"),
        "accNumber" => I("post.accNumber"),
        "idList" => I("post.idList"),
        "widthList" => I("post.widthList"),
      ];

      $service = new AccQueryService();
      $this->ajaxReturn($service->saveViewLayout($params));
    }
  }

  /**
   * 查询一级科目列表
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\AccQuery\GlMainForm.js
   */
  public function subjectList()
  {
    if (IS_POST) {
      $us = new UserService();

      if (!$us->hasPermission(FIdConst::GL_ACC_QUERY_GL)) {
        die("没有权限");
      }

      $params = [
        "companyId" => I("post.companyId"),
        "subjectCode" => I("post.subjectCode"), // 模糊查询的科目码
      ];

      $service = new AccQueryService();
      $this->ajaxReturn($service->subjectList($params));
    }
  }

  /**
   * 查询总分类账数据
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\AccQuery\GlMainForm.js
   */
  public function queryAccGlData()
  {
    if (IS_POST) {
      $us = new UserService();

      if (!$us->hasPermission(FIdConst::GL_ACC_QUERY_GL)) {
        die("没有权限");
      }

      $params = [
        "companyId" => I("post.companyId"),
        "subjectCode" => I("post.subjectCode"),
        "year" => I("post.year"),
        "month" => I("post.month"),
      ];

      $service = new AccQueryService();
      $this->ajaxReturn($service->queryAccGlData($params));
    }
  }

  /**
   * 查询凭证id，用于查看凭证
   * 
   * JS：web\Public\Scripts\PSI\SLN0002\AccQuery\DetailMainForm.js
   */
  public function queryVoucherId()
  {
    if (IS_POST) {
      $us = new UserService();

      if (!$us->hasPermission(FIdConst::GL_ACC_QUERY_GL)) {
        die("没有权限");
      }

      $params = [
        "companyId" => I("post.companyId"),
        "year" => I("post.year"),
        "month" => I("post.month"),
        "wrod" => I("post.word"), // 凭证字
        "num" => I("post.num"), // 凭证号
      ];

      $service = new AccQueryService();
      $this->ajaxReturn($service->queryVoucherId($params));
    }
  }
}
